package com.aeoj.clientmanagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.aeoj.clientmanagement.domain.problemrecord.ProblemRecord;
import com.aeoj.clientmanagement.domain.problemrecord.dto.ProblemRecordSearchDTO;
import com.aeoj.clientmanagement.domain.problemrecord.dto.ProblemRecordUpdateDTO;
import com.aeoj.clientmanagement.domain.problemrecord.vo.ProblemRecordVO;
import com.aeoj.clientmanagement.domain.response.R;
import com.aeoj.clientmanagement.domain.user.vo.SysUserVO;
import com.aeoj.clientmanagement.enums.ResponseCodeEnum;
import com.aeoj.clientmanagement.exception.BusinessException;
import com.aeoj.clientmanagement.service.ProblemRecordService;
import com.aeoj.clientmanagement.utils.user.UserUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ProblemRecordController
 * author: ZSM
 * time: 2025-04-22 14:03
 */
@RestController
@RequestMapping("/client/problem_record")
public class ProblemRecordController {
    @Resource
    private ProblemRecordService problemRecordService;

    /**
     * 获取提交记录列表
     * @param dto
     * @param request
     * @return
     */
    @GetMapping("list")
    public R list(ProblemRecordSearchDTO dto, HttpServletRequest request) {
        UserUtils.getLoginUser(request);
        Page<ProblemRecord> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        QueryWrapper<ProblemRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        List<ProblemRecord> list = problemRecordService.list(page, queryWrapper);
        List<ProblemRecordVO> voList = new ArrayList<>();
        list.forEach(x -> {
            voList.add(ProblemRecordVO.toProblemRecordVO(x));
        });

        Page<ProblemRecordVO> res = new Page<>();
        res.setRecords(voList);
        res.setTotal(page.getTotal());

        return R.ok(res);
    }

    /**
     * 获取提交记录详情
     * @param id
     * @param request
     * @return
     */
    @GetMapping("get")
    public R get(Long id, HttpServletRequest request) {
        UserUtils.getLoginUser(request);
        if (id == null || id <= 0) {
            throw new BusinessException(ResponseCodeEnum.BAD_REQUEST);
        }

        return R.ok(problemRecordService.getById(id));
    }

    /**
     * 修改提交记录备注
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("update")
    public R update(@RequestBody ProblemRecordUpdateDTO dto, HttpServletRequest request) {
        SysUserVO loginUser = UserUtils.getLoginUser(request);

        Long problemRecordId = dto.getProblemRecordId();
        if (problemRecordId == null || problemRecordId <= 0) {
            throw new BusinessException(ResponseCodeEnum.BAD_REQUEST);
        }

        ProblemRecord problemRecord = problemRecordService.getById(problemRecordId);

        problemRecord.setRemark(dto.getRemark());
        boolean b = problemRecordService.updateById(problemRecord);
        if (b)
            return R.ok();
        return R.error(ResponseCodeEnum.BUSINESS_ERROR);
    }
}
